<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/6
 * Time: 8:32
 */

/**
 * 获取当前页面URL
 * @return string
 */
function curPageURL()
{
    $pageURL = 'http';

    if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on")
    {
        $pageURL .= "s";
    }
    $pageURL .= "://";

    if ($_SERVER["SERVER_PORT"] != "80")
    {
        $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
    }
    else
    {
        $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}

/**
 * 对当然url进行配置后进行301转让发
 * @param $_rule //定义要301的url,该对象必需是如下格式array(['正则','url'],['正则','url'],...)
 */
function seoLocation($_rule){
        $curlpage=curPageURL();$url='';
        foreach ($_rule as $key => $value) {
            if (preg_match ($value[0],$curlpage, $matches )) {
                //print_r($matches);exit('//测试的时候可以开启这行');
                $i=0;$url=$value['1'];
                foreach ($matches as $t_k => $t_v) {
                    if($i!=0){
                        $url= str_replace('{{$s'.$t_k.'}}',$t_v,$url);
                        $url= str_replace('{{idEncryption($s'.$t_k.')}}',idEncryption($t_v),$url);
                    }
                    $i++;
                }
                if($url){
                    header("HTTP/1.1 301 Moved Permanently");
                    header("Location:".$url);
                    exit;
                }
            }
        }
}

//定义要301的url
$_rule=array(
    //职位详情
    array("/corporation\/jobinfo\.php\?ac_id=[a-zA-Z0-9]*_[0-9]*&aj_id=[a-zA-Z0-9]*_([0-9]*?)/iU",'/jobinfo_{{idEncryption($s1)}}.html'),
    //公司详情
    array("/corporation\/com_about\.php\?ac_id=[a-zA-Z0-9]*_([0-9]*?)/iU",'/comabout_{{idEncryption($s1)}}.html'),//{{$s1}}
    //简历详情
    array("/job\/preview_resume\.php\?id=([0-9]*?)/iU",'/resume_{{idEncryption($s1)}}.html'),
    //咨询详情
    array("/news\/new_show\.php\?id=([0-9]*?)/iU",'/newsinfo_{{$s1}}.html'),
    //找工作
    array("/job\/post_search\.php(.*)/iU",'/job'),
    //找人才
    array("/corporation\/resume_search\.php(.*)/iU",'/personnel')
);
seoLocation($_rule);